﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jsptpd.Common.DB;

namespace Jsptpd.JobImpl
{
    public class wfDataOperation
    {
        private log4net.ILog logger = log4net.LogManager.GetLogger(typeof(Jsptpd.JobImpl.wfDataOperation));
        
        private DataSet getOverTimeData()
        {             
            StringBuilder sb = new StringBuilder();
            sb.Append("select distinct t.task_recipientid");
            sb.Append(" from jsptpd_sys..sys_fl_task t");
            sb.Append(" where task_endtime is null ");
            sb.Append(" and datediff(MINUTE, convert(varchar(22),t.task_recievedate,120),CONVERT(varchar(22), GETDATE(), 120))>2880");
            DBHelper dh = new DBHelper();
            DataSet ds = dh.GetDS(sb.ToString());
            if (ds == null)
            {
                logger.Info("Everybody deal workflow well,no one need to notify......");
            }
            return ds;
        }

        public void wfNotify()
        {
            string msginfo = "您有待办任务超过两天未办理，请您及时查看！";
            DataSet ds = getOverTimeData();
            if (ds != null)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (!string.IsNullOrEmpty(dr["task_recipientid"].ToString()))
                    {
                        string userName = getUserName(Convert.ToInt32(dr["task_recipientid"]));
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("insert into jsptpd_sys..sys_rtxnotify");
                        sb.AppendLine("(username,title,msginfo,row_guid,wf_guid,proc_guid)");
                        sb.AppendLine("values('" + userName + "','提示信息','" + msginfo);
                        sb.AppendLine("',QUOTENAME(newid(),'{'),QUOTENAME(newid(),'{'),QUOTENAME(newid(),'{'))");
                        logger.Info("The insert statement is:"+sb.ToString());
                        DBHelper dh = new DBHelper();
                        Int32 rowsAffected = dh.executeNonQuery(sb.ToString());
                        if (rowsAffected == 1)
                        {
                            logger.Info("已给" + userName + "流程超时提醒");
                        }
                        else
                        {
                            logger.Error(userName + "流程超时提醒异常");
                        }
                    }
                    else
                    {
                        logger.Error("接收用户为空");
                    }
                }
            }
        }

        private string getUserName(int userId)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("select st_name");
            sb.AppendLine("from jsptpd_sys..sys_staffdef");
            sb.AppendLine("where st_id=" + userId);
            DBHelper dh = new DBHelper();
            DataSet ds = dh.GetDS(sb.ToString());
            if (ds.Tables[0].Rows.Count > 1)
            {
                logger.Error("存在重复的人名......");
                return "";
            }
            if (ds.Tables[0].Rows.Count < 1)
            {
                logger.Error("在流程提醒时获取用户人名失败");
                return "";
            }
            return ds.Tables[0].Rows[0]["st_name"].ToString();
        }
    }
}
